package Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import ModelBean.RegisterBean;
import POJO.Cart;
import POJO.CartDetail;
import POJO.ProductDetail;
import Service.HomeService;
import ServiceImp.CreateData;

@Controller
@Scope("request")
public class HomeController {
	
	@Autowired
	private HomeService homeService;
	
	@Autowired
	private CreateData createData;
	
	@Autowired
	private Cart cart;
	
	@RequestMapping("/home")
	public ModelAndView showInfo()
	{
		Map<String, Object> model = new HashMap<String, Object>();
		
		//createData.generateData();
		
		model.put("newmodel", homeService.createTreeMenu());
		model.put("productDiscount", homeService.getProductDiscount(8));
		List<ProductDetail> l = homeService.getProductNew(8);
		//System.out.println(l.size());
		model.put("productNew", l);
		
		cart.getCartDetails().add(new CartDetail());
		
		return new ModelAndView("home", model);
	}
	
	
	@RequestMapping(value = "/detail", method = RequestMethod.GET)
	public ModelAndView showDetail(@RequestParam("id") int id)
	{
		System.out.println("==============detail================" + cart.getCartDetails().size());
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());		
		//System.out.println("detail "+id);
		model.put("productDetail", homeService.getProductById(id));
		model.put("count", homeService.getCountInStore(id));
		model.put("types", homeService.getTypes());
		model.put("navi", homeService.createNavi(id, 2));
		return new ModelAndView("detail", model);
	}

	
	@RequestMapping(value = "/detailcategory", method = RequestMethod.GET)
	public ModelAndView showDetailCategory(@RequestParam("id") int id)
	{
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());	
		model.put("navi", homeService.createNavi(id, 1));
		//System.out.println("detail "+id);
		model.put("category", homeService.getCategoryById(id));
		return new ModelAndView("detailcategory", model);
	}
	
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(@RequestParam(value = "error", required = false) String error,
			@RequestParam(value = "logout", required = false) String logout, HttpServletRequest request){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		return new ModelAndView("login", model);
	}
	
	@RequestMapping(value = "/reg")
	public ModelAndView showRegisterForm(@ModelAttribute("command") RegisterBean register){
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("newmodel", homeService.createTreeMenu());
		return new ModelAndView("register", model);
	}
	
	@RequestMapping(value = "/register", method = RequestMethod.POST)
	public ModelAndView register(@ModelAttribute("command") RegisterBean register, 
			BindingResult result) {
		int customerId = homeService.createUser(register);
		return new ModelAndView("login");
	}
}






